﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;
using System.Collections;


/// <summary>
/// Hello World!!
/// </summary>
public class GameManager : MonoBehaviour
{
    
    protected Dictionary<string, Player> players = new Dictionary<string, Player>();

    public static System.Action<string> playerMove;
    public static System.Action<string> playerRota;
    public static System.Action<string> playerFire;
    public static System.Action<User> playerLogOut;


    public static System.Action<UserData> playerPosition;

    public GameObject tank;
    Player localPlayer;

    void Awake()
    {
        LoginPanel.loginSuccess += OnLogin;
        playerMove += PlayerMove;
        playerRota += PlayerRota;
        playerFire += PlayerFire;
        playerPosition += PlayerPos;
        playerLogOut += PlayerLogOut;
    }

    private void OnDestroy()
    {
        LoginPanel.loginSuccess -= OnLogin;
        playerMove -= PlayerMove;
        playerRota -= PlayerRota;
        playerFire -= PlayerFire;
        playerPosition -= PlayerPos;
        playerLogOut -= PlayerLogOut;

    }

    void OnLogin(string json, bool islocal)
    {
        UserData user = JsonUtility.FromJson<UserData>(json);

        //Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        //string username = dic["username"];
        //Color color = JsonUtility.FromJson<Color>(dic["color"]);

        Player player = CreatePlayer(user);

        //是自己的玩家
        if (islocal)
        {
            GameObject.FindObjectOfType<LocalController>().SetLocol(player);
            localPlayer = player;
        }
        else
        {
            //发送自己的位置给服务器
            localPlayer.userData.pos = localPlayer.transform.position;
            string jsonstr = JsonUtility.ToJson(localPlayer.userData);
            //user.pos = localPlayer.transform.position;//新玩家的位置，不是自己的位置
            NetworkManager.Instance.SendMessage(Protocol.PlayerPosition_CMD, jsonstr);
        }
    }

    void PlayerMove(string json)
    {
        Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        string username = dic["username"];
        Vector3 dir = JsonUtility.FromJson<Vector3>(dic["dir"]);

        //指定玩家移动
        players[username].Move(dir);
    }
    void PlayerRota(string json)
    {
        Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        string username = dic["username"];
        Vector3 rota = JsonUtility.FromJson<Vector3>(dic["rota"]);
        players[username].Rota(rota);
    }

    void PlayerFire(string json)
    {
        Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        string name = dic["username"];
        double time = double.Parse(dic["fireTime"]);
        players[name].Fire(time);
    }

    void PlayerLogOut(User user)
    {
        if (players.ContainsKey(user.username))
        {
            //销毁玩家
            Player player = players[user.username];
            Destroy(player.gameObject);

            //删除字典的引用
            players.Remove(user.username);
        }

    }

    void PlayerPos(UserData user)
    {
        if (players.ContainsKey(user.username))
        {
            players[user.username].transform.position = user.pos;
        }
        else
        {
            CreatePlayer(user);
        }
    }

    Player CreatePlayer(UserData user)
    {
        //创建游戏对象
        //GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        GameObject go = Instantiate(tank);
        go.transform.position = user.pos;
        //go.GetComponent<MeshRenderer>().material.color = user.color;
        go.transform.GetChild(0).transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(0).transform.GetChild(1).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(0).transform.GetChild(2).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(0).transform.GetChild(3).GetComponent<MeshRenderer>().materials[0].color = user.color;


        Player player = go.AddComponent<Player>();
        player.userData = user;

        players.Add(user.username, player);
        return player;
    }

}
